##### The Divergent Effects of COVID-19 on Attitudes toward Democracy: Evidence from South Korea #####

library(haven)
library(tidyverse)
library(AER)
library(stargazer)
library(plm)
library(reshape2)
library(writexl)
library(ggthemes)
library(readxl)
library(psych)
library(MASS)
library(DescTools)

### Figures

## Figure 1

d.world <- read.csv("Deposit-VIC.csv")

d.comp <- d.world %>% group_by(cname) %>% 
  dplyr::summarise(mean = round(mean(healthcon_n, na.rm = T), 2)) %>% 
  mutate(lab = format(mean, nsmall = 2)) %>% 
  arrange(desc(mean)) %>% data.frame()

fig1 <- ggplot(data = d.comp, aes(x = cname, y = mean)) +
  geom_bar(stat = "identity", position = "dodge2") +
  labs(x = "", y = "Mean") +
  theme_bw() +
  scale_x_discrete(limits = d.comp$cname,
                     breaks = d.comp$cname,
                     labels = d.comp$cname) +
  scale_y_continuous(limits = c(-0.02, 5.02),
                     breaks = c(0, 1, 2, 3, 4, 5)) +
  theme(aspect.ratio = 0.5,
        axis.text.x = element_text(angle = 90, vjust = 0.5, size = 10),
        axis.text.y = element_text(size = 10)) +
  geom_text(aes(label = lab),  position = position_dodge(width = 0.9), vjust = -0.8) 


ggsave("Fig1.png", fig1, width = 11)

## Figure 2

d.aprv <- data.frame(read_xlsx("Deposit-Approval-Moon.xlsx"))

fig2 <- ggplot(data = d.aprv, aes(x = no, y = president)) +
  geom_line(color = "black") +
  labs(x = "", y = "Approval Rating (%)") +
  theme_bw() +
  scale_x_continuous(limits = c(0.8, 20.2),
                     breaks = 1:20,
                     labels = d.aprv$week) +
  scale_y_continuous(limits = c(-0.02, 100.02),
                     breaks = c(0, 25, 50, 75, 100)) +
  theme(aspect.ratio = 0.5,
        axis.text.x = element_text(angle = 90, vjust = 0.5, size = 10),
        axis.text.y = element_text(size = 10))

ggsave("Fig2.png", fig2, width = 11)

### Tables

dl <- readRDS("Deposit-Long-full.rds")
dl2 <- readRDS("Deposit-Long-part.rds")
dw <- readRDS("Deposit-Wide.rds")

## Table 1 & Table A3

t1.1 <- lm(satis_1 ~ covidrisk_1 + cpcity_1 +
            polid_1 + party_1 + region_1 +
            income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
            married_1,
          data = dw)
summary(t1.1)

t1.2 <- lm(asmbly_1 ~ covidrisk_1 + cpcity_1 +
            polid_1 + party_1 + region_1 +
            income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
            married_1,
          data = dw)
summary(t1.2)

ta3.1 <- polr(asmbly_c_1 ~ covidrisk_1 + cpcity_1 +
               polid_1 + party_1 + region_1 +
               income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
            married_1,
          data = dw, Hess = T)
summary(ta3.1)

ta3.1$AIC <- AIC(ta3.1)
ta3.1$r.squared <- PseudoR2(ta3.1)

t1.3 <- lm(track_1 ~ covidrisk_1 + cpcity_1 +
            polid_1 + party_1 + region_1 +
            income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
            married_1,
          data = dw)
summary(t1.3)

ta3.2 <- polr(track_c_1 ~ covidrisk_1 + cpcity_1 +
               polid_1 + party_1 + region_1 +
               income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
            married_1,
          data = dw, Hess = T)
summary(ta3.2)

ta3.2$AIC <- AIC(ta3.2)
ta3.2$r.squared <- PseudoR2(ta3.2)

# Table 1 - out

cov1 <- c("Perceived Risk", "Capacity Evaluation",
          "Political Ideology", "Party: DPK", "Party: PPP", "Party: Other",
          "Region: Honam", "Region: Youngnam",
          "Household Income", "Household Assets", "Gender: Female", "Age", "Education", 
          "Marital Status: Married")

stargazer(t1.1, t1.2, t1.3,
          digits = 3,
          digits.extra = 0,
          type = "html", style = "apsr",
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"),
          covariate.labels = cov1,
          dep.var.labels = c("Satisfaction with Democracy",
            "Public Gathering Prohibition", "Monitoring Personal Data and Communications"),
          keep.stat = c("rsq", "adj.rsq", "n"),
          out = "Table1.doc")

# Table A3 - out

stargazer(ta3.1, ta3.2,
          digits = 3,
          digits.extra = 0,
          type = "html", style = "apsr",
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"),
          covariate.labels = cov1,
          dep.var.labels = c("Public Gathering Prohibition", 
                             "Monitoring Personal Data and Communications"),
          keep.stat = c("aic", "rsq", "n"),
          out = "TableA3.doc")

## Table 2

t2.1 <- lm(satis ~ covidrisk_dn + satis_lag + cpcity_1 + 
             polid_1 + party_1 + region_1 +
             income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
             married_1 + seq,
           data = dl)
coeftest(t2.1, vcov = vcovHC(t2.1, type = "HC1", cluster ="no"))

t2.1a <- lm(satis ~ covidrisk_dn + satis_lag + cpcity_1 + 
             polid_1 + party_1 + region_1 +
             income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
             married_1 + seq,
           data = dl2)
coeftest(t2.1a, vcov = vcovHC(t2.1a, type = "HC1", cluster ="no"))

t2.2 <- lm(asmbly ~ covidrisk_dn + asmbly_lag + cpcity_1 +  
             polid_1 + party_1 + region_1 +
             income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
             married_1 + seq,
           data = dl)
coeftest(t2.2, vcov = vcovHC(t2.2, type = "HC1", cluster="no"))

t2.2a <- lm(asmbly ~ covidrisk_dn + asmbly_lag + cpcity_1 +  
             polid_1 + party_1 + region_1 +
             income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
             married_1 + seq,
           data = dl2)
coeftest(t2.2a, vcov = vcovHC(t2.2a, type = "HC1", cluster="no"))

t2.3 <- lm(track ~ covidrisk_dn + track_lag + cpcity_1 + 
             polid_1 + party_1 + region_1 +
             income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
             married_1 + seq,
           data = dl)
coeftest(t2.3, vcov = vcovHC(t2.3, type = "HC1", cluster="no"))

t2.3a <- lm(track ~ covidrisk_dn + track_lag + cpcity_1 + 
             polid_1 + party_1 + region_1 +
             income_rs_1 + asset_1 + gender_1 + age_1 + educ_1 + 
             married_1 + seq,
           data = dl2)
coeftest(t2.3a, vcov = vcovHC(t2.3a, type = "HC1", cluster="no"))

# Table 2 - out

cov2 <- c("Perceived Risk: Down", 
          "Satisfaction with Democracy t-1",
          "Public Gathering Prohibition t-1",
          "Monitoring Personal Data and Communications t-1",
          "Capacity Evaluation",
          "Political Ideology", "Party: DPK", "Party: PPP", "Party: Other",
          "Region: Honam", "Region: Youngnam",
          "Household Income", "Household Assets", "Gender: Female", 
          "Age", "Education", "Marital Status: Married",
          "Wave 3")
          
robse.t2 <- list(sqrt(diag(vcovHC(t2.1, type = "HC1", cluster = "no"))),
                 sqrt(diag(vcovHC(t2.1a, type = "HC1", cluster = "no"))),
                 sqrt(diag(vcovHC(t2.2, type = "HC1", cluster = "no"))),
                 sqrt(diag(vcovHC(t2.2a, type = "HC1", cluster = "no"))),
                 sqrt(diag(vcovHC(t2.3, type = "HC1", cluster = "no"))),
                 sqrt(diag(vcovHC(t2.3a, type = "HC1", cluster = "no"))))


stargazer(t2.1, t2.1a, t2.2, t2.2a, t2.3, t2.3a,  
          digits = 3,
          digits.extra = 0,
          se = robse.t2,
          type = "html", style = "apsr",
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"),
          covariate.labels = cov2,
          dep.var.labels = c("Satisfaction with Democracy",
            "Public Gathering Prohibition", "Monitoring Personal Data and Communications"),
          keep.stat = c("rsq", "adj.rsq", "n"),
          out = "Table2.doc")

### Appendix

## Table A1

ta1.satis1a <- dw %>% dplyr::select(satis_r_1) %>% psych::describe() %>% 
                     as.data.frame() %>% dplyr::select(min, max)

ta1.satis1a <- ta1.satis1a %>% 
  transmute(
    type = "DV",
    variable = "Satisfaction with Democracy",
    value = "Min, Max",
    num1 = round(min, 1),
    num2 = round(max, 1))
    
ta1.satis1b <- dw %>% dplyr::select(satis_r_1) %>% psych::describe() %>% 
  as.data.frame() %>% dplyr::select(mean, sd)

ta1.satis1b <- ta1.satis1b %>% 
  transmute(
    type = "DV",
    variable = "Satisfaction with Democracy",
    value = "Mean, SD",
    num1 = round(mean, 1),
    num2 = round(sd, 1))
    
ta1.satis1 <- rbind(ta1.satis1a, ta1.satis1b)

ta1.satis2a <- dw %>% dplyr::select(satis_r_2) %>% psych::describe() %>% 
  as.data.frame() %>% dplyr::select(min, max)

ta1.satis2a <- ta1.satis2a %>% 
  transmute(
    type = "DV",
    variable = "Satisfaction with Democracy",
    value = "Min, Max",
    num1 = round(min, 1),
    num2 = round(max, 1))

ta1.satis2b <- dw %>% dplyr::select(satis_r_2) %>% psych::describe() %>% 
  as.data.frame() %>% dplyr::select(mean, sd)

ta1.satis2b <- ta1.satis2b %>% 
  transmute(
    type = "DV",
    variable = "Satisfaction with Democracy",
    value = "Mean, SD",
    num1 = round(mean, 1),
    num2 = round(sd, 1))

ta1.satis2 <- rbind(ta1.satis2a, ta1.satis2b)

ta1.satis3a <- dw %>% dplyr::select(satis_r_3) %>% psych::describe() %>% 
  as.data.frame() %>% dplyr::select(min, max)

ta1.satis3a <- ta1.satis3a %>% 
  transmute(
    type = "DV",
    variable = "Satisfaction with Democracy",
    value = "Min, Max",
    num1 = round(min, 1),
    num2 = round(max, 1))

ta1.satis3b <- dw %>% dplyr::select(satis_r_3) %>% psych::describe() %>% 
  as.data.frame() %>% dplyr::select(mean, sd)

ta1.satis3b <- ta1.satis3b %>% 
  transmute(
    type = "DV",
    variable = "Satisfaction with Democracy",
    value = "Mean, SD",
    num1 = round(mean, 1),
    num2 = round(sd, 1))

ta1.satis3 <- rbind(ta1.satis3a, ta1.satis3b)

ta1.asmbly1 <- dw %>% group_by(asmbly_c_1) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "DV",
    variable = "Public Gathering Prohibition",
    value = c("Strongly disagree", "Somewhat disagree",
              "Somewhat agree", "Strongly agree"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.asmbly2 <- dw %>% group_by(asmbly_c_2) %>% filter(!is.na(age_2)) %>%  
  summarise(n = n()) %>% 
  transmute(
    type = "DV",
    variable = "Public Gathering Prohibition",
    value = c("Strongly disagree", "Somewhat disagree",
              "Somewhat agree", "Strongly agree"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.asmbly3 <- dw %>% group_by(asmbly_c_3) %>% filter(!is.na(age_3)) %>%  
  summarise(n = n()) %>% 
  transmute(
    type = "DV",
    variable = "Public Gathering Prohibition",
    value = c("Strongly disagree", "Somewhat disagree",
              "Somewhat agree", "Strongly agree"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))


ta1.track1 <- dw %>% group_by(track_c_1) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "DV",
    variable = "Monitoring Personal Data and Communications",
    value = c("Strongly disagree", "Somewhat disagree",
              "Somewhat agree", "Strongly agree"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.track2 <- dw %>% group_by(track_c_2) %>% filter(!is.na(age_2)) %>%  
  summarise(n = n()) %>% 
  transmute(
    type = "DV",
    variable = "Monitoring Personal Data and Communications",
    value = c("Strongly disagree", "Somewhat disagree",
              "Somewhat agree", "Strongly agree"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.track3 <- dw %>% group_by(track_c_3) %>% filter(!is.na(age_3)) %>%  
  summarise(n = n()) %>% 
  transmute(
    type = "DV",
    variable = "Monitoring Personal Data and Communications",
    value = c("Strongly disagree", "Somewhat disagree",
              "Somewhat agree", "Strongly agree"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))


ta1.covidrisk1 <- dw %>% group_by(covidrisk_r_1) %>% 
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Perceived Risk",
    value = c("Not at all afraid", "Not very afraid",
              "Neither, nor", "Quite afraid", "Very afraid"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.covidrisk2 <- dw %>% group_by(covidrisk_r_2) %>% filter(!is.na(age_2)) %>%  
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Perceived Risk",
    value = c("Not at all afraid", "Not very afraid",
              "Neither, nor", "Quite afraid", "Very afraid"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.covidrisk3 <- dw %>% group_by(covidrisk_r_3) %>% filter(!is.na(age_3)) %>%  
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Perceived Risk",
    value = c("Not at all afraid", "Not very afraid",
              "Neither, nor", "Quite afraid", "Very afraid"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))


ta1.cpcity1 <- dw %>% group_by(cpcity_r_1) %>% 
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Capacity Evaluation",
    value = c("Very poorly", "Quite poorly",
              "Neither, nor", "Quite well", "Very well"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.cpcity2 <- dw %>% group_by(cpcity_r_2) %>% filter(!is.na(age_2)) %>%  
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Capacity Evaluation",
    value = c("Very poorly", "Quite poorly",
              "Neither, nor", "Quite well", "Very well"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.cpcity3 <- dw %>% group_by(cpcity_r_3) %>% filter(!is.na(age_3)) %>%  
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Capacity Evaluation",
    value = c("Very poorly", "Quite poorly",
              "Neither, nor", "Quite well", "Very well"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.covidriskdn1 <- data.frame(
    type = "IV",
    variable = "Changes in Perceived Risk",
    value = c("Down", "Other"),
    num1 = NA,
    num2 = NA)

ta1.covidriskdn2 <- dw %>% group_by(covidrisk_dn_2) %>% filter(!is.na(age_2)) %>%  
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Changes in Perceived Risk",
    value = c("Down", "Other"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.covidriskdn3 <- dw %>% group_by(covidrisk_dn_3) %>% filter(!is.na(age_3)) %>%  
  summarise(n = n()) %>%  
  transmute(
    type = "IV",
    variable = "Changes in Perceived Risk",
    value = c("Down", "Other"),
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta1.total1 <- data.frame(
   type = "Total",
   variable = "",
   value = "",
   num1 = "2544",
   num2 = "100%"
 )
 
 ta1.total2 <- data.frame(
   type = "Total",
   variable = "",
   value = "",
   num1 = "1832",
   num2 = "100%"
 )
 
 ta1.total3 <- data.frame(
   type = "Total",
   variable = "",
   value = "",
   num1 = "1078",
   num2 = "100%"
 )

ta1.1 <- rbind(ta1.satis1, ta1.asmbly1, ta1.track1, 
                ta1.covidrisk1, ta1.cpcity1, ta1.covidriskdn1, ta1.total1)
 
ta1.2 <- rbind(ta1.satis2, ta1.asmbly2, ta1.track2, 
               ta1.covidrisk2, ta1.cpcity2, ta1.covidriskdn2, ta1.total2)

ta1.3 <- rbind(ta1.satis3, ta1.asmbly3, ta1.track3, 
               ta1.covidrisk3, ta1.cpcity3, ta1.covidriskdn3, ta1.total3)


ta1 <- cbind(ta1.1, ta1.2$num1, ta1.2$num2, ta1.3$num1, ta1.3$num2)
ta1 
writexl::write_xlsx(ta1, "TableA1.xlsx")

## Table A2 

ta2.party1 <- dw %>% 
  group_by(party_1) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Party Identification",
    value = party_1,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))


ta2.party2 <- dw %>% filter(!is.na(age_2)) %>%
  group_by(party_2) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Party Identification",
    value = party_2,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.party3 <- dw %>% filter(!is.na(age_3)) %>%
  group_by(party_3) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Party Identification",
    value = party_3,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))


ta2.region1 <- dw %>% 
  group_by(region_1) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Region",
    value = region_1,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))


ta2.region2 <- dw %>% filter(!is.na(age_2)) %>%
  group_by(region_2) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Region",
    value = region_2,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.region3 <- dw %>% filter(!is.na(age_3)) %>%
  group_by(region_3) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Region",
    value = region_3,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))


ta2.gender1 <- dw %>% 
  group_by(gender_1) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Gender",
    value = gender_1,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.gender2 <- dw %>% filter(!is.na(age_2)) %>%
  group_by(gender_2) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Gender",
    value = gender_2,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.gender3 <- dw %>% filter(!is.na(age_3)) %>%
  group_by(gender_3) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Gender",
    value = gender_3,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.married1 <- dw %>% 
  group_by(married_1) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Marital Status",
    value = married_1,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.married2 <- dw %>% filter(!is.na(age_2)) %>%
  group_by(married_2) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Marital Status",
    value = married_2,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.married3 <- dw %>% filter(!is.na(age_3)) %>%
  group_by(married_3) %>% 
  summarise(n = n()) %>% 
  transmute(
    type = "Control",
    variable = "Marital Status",
    value = married_3,
    num1 = n,
    num2 = paste(format(round(n / sum(n)*100, 1), nsmall = 1), "%", sep = ""))

ta2.con1a <- dw %>% dplyr::select(income_1, asset_1, polid_1, age_1, educ_1) %>% 
  psych::describe() %>% as.data.frame() %>% 
  dplyr::select(vars, min, max)

ta2.con1a <- ta2.con1a %>% 
  transmute(
    type = "Control",
    variable = c("Household Income", "Household Assets", "Political Ideology",
                 "Age", "Education"),
    value = "Min, Max",
    num1 = round(min, 1),
    num2 = round(max, 1),
    vars = vars)

ta2.con1b <- dw %>% dplyr::select(income_1, asset_1, polid_1, age_1, educ_1) %>% 
  psych::describe() %>% as.data.frame() %>% 
  dplyr::select(vars, mean, sd)

ta2.con1b <- ta2.con1b %>% 
  transmute(
    type = "Control",
    variable = c("Household Income", "Household Assets", "Political Ideology",
                 "Age", "Education"),
    value = "Mean, SD",
    num1 = round(mean, 1),
    num2 = round(sd, 1),
    vars = vars)

ta2.con1 <- rbind(ta2.con1a, ta2.con1b) %>% arrange(vars) %>% dplyr::select(-vars)

ta2.con2a <- dw %>% dplyr::select(income_2, asset_2, polid_2, age_2, educ_2) %>% 
  psych::describe() %>% as.data.frame() %>% 
  dplyr::select(vars, min, max)

ta2.con2a <- ta2.con2a %>% 
  transmute(
    type = "Control",
    variable = c("Household Income", "Household Assets", "Political Ideology",
                 "Age", "Education"),
    value = "Min, Max",
    num1 = round(min, 1),
    num2 = round(max, 1),
    vars = vars)

ta2.con2b <- dw %>% dplyr::select(income_2, asset_2, polid_2, age_2, educ_2) %>% 
  psych::describe() %>% as.data.frame() %>% 
  dplyr::select(vars, mean, sd)

ta2.con2b <- ta2.con2b %>% 
  transmute(
    type = "Control",
    variable = c("Household Income", "Household Assets", "Political Ideology",
                 "Age", "Education"),
    value = "Mean, SD",
    num1 = round(mean, 1),
    num2 = round(sd, 1),
    vars = vars)

ta2.con2 <- rbind(ta2.con2a, ta2.con2b) %>% arrange(vars) %>% dplyr::select(-vars)

ta2.con3a <- dw %>% dplyr::select(income_3, asset_3, polid_3, age_3, educ_3) %>% 
  psych::describe() %>% as.data.frame() %>% 
  dplyr::select(vars, min, max)

ta2.con3a <- ta2.con3a %>% 
  transmute(
    type = "Control",
    variable = c("Household Income", "Household Assets", "Political Ideology",
                 "Age", "Education"),
    value = "Min, Max",
    num1 = round(min, 1),
    num2 = round(max, 1),
    vars = vars)

ta2.con3b <- dw %>% dplyr::select(income_3, asset_3, polid_3, age_3, educ_3) %>% 
  psych::describe() %>% as.data.frame() %>% 
  dplyr::select(vars, mean, sd)

ta2.con3b <- ta2.con3b %>% 
  transmute(
    type = "Control",
    variable = c("Household Income", "Household Assets", "Political Ideology",
                 "Age", "Education"),
    value = "Mean, SD",
    num1 = round(mean, 1),
    num2 = round(sd, 1),
    vars = vars)

ta2.con3 <- rbind(ta2.con3a, ta2.con3b) %>% arrange(vars) %>% dplyr::select(-vars)

ta2.total1 <- data.frame(
  type = "Total",
  variable = "",
  value = "",
  num1 = "2544",
  num2 = "100%"
)

ta2.total2 <- data.frame(
  type = "Total",
  variable = "",
  value = "",
  num1 = "1832",
  num2 = "100%"
)

ta2.total3 <- data.frame(
  type = "Total",
  variable = "",
  value = "",
  num1 = "1078",
  num2 = "100%"
)

ta2.1 <- rbind(ta2.party1, ta2.region1, ta2.gender1, 
               ta2.married1, ta2.con1, ta2.total1)

ta2.2 <- rbind(ta2.party2, ta2.region2, ta2.gender2, 
               ta2.married2, ta2.con2, ta2.total2)

ta2.3 <- rbind(ta2.party3, ta2.region3, ta2.gender3, 
               ta2.married3, ta2.con3, ta2.total3)


ta2 <- cbind(ta2.1, ta2.2$num1, ta2.2$num2, ta2.3$num1, ta2.3$num2)

writexl::write_xlsx(ta2, "TableA2.xlsx")
